//
//  TestViewController.m
//  HuaRenWang
//
//  Created by 马文涛 on 13-8-21.
//  Copyright (c) 2013年 qhose. All rights reserved.
//

#import "Tool.h"
#import "Common.h"

@implementation Tool

// 复制内容到剪贴板
+ (void)copyInfo:(NSString*)content
{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = content;
}

/**************************************************
 * 描述：用某一个字符替换原字符串中多个字符
 * 参数：beforeStr,原字符串;strAry,要替换多个字符;str,新替换的字符
 * 返回：新字符串
 * 作者：马文涛
 * 创建时间：20130108
 ***************************************************/
+ (NSString *)stringByReplacingArrayWithString:(NSString *)beforeStr stringAry:(NSArray *)strAry withString:(NSString *)str
{
    NSString *newStr = nil;
    const char *c = [beforeStr cStringUsingEncoding:NSUTF8StringEncoding];
    
    // 替换C字符串中字符
    int len = strlen(c);
    char *cStr = malloc(len + 1);
    char *tempCstr = cStr;
    if (cStr) {
        strcpy(cStr, c);
        while (len--) {
            for (NSString *nsStr in strAry) {
               const char *temp = [nsStr cStringUsingEncoding:NSUTF8StringEncoding];
                if (*cStr == *temp) {
                    *cStr = '_';
                }
            }
            cStr++;
        }
        newStr = [NSString stringWithUTF8String:tempCstr];
    }
    
    return newStr;
}

// 返回一个纯色的图片
+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
